package com.lesserpand.lspand.story.item.controller;

import com.lesserpand.lspand.story.item.entity.StudentDTO;
import com.lesserpand.lspand.story.item.entity.StudentQuery;
import com.lesserpand.lspand.story.item.service.StudentService;
import io.searchbox.client.JestResult;
import io.searchbox.core.SearchResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author bailuo
 * @version 0.0.1
 * @package com.lesserpand.lspand.story.item.controller
 * @description 学习es controller
 * @date Created in 2018/6/11 11:32
 * @company
 * @copyright Copyright (c) 2017
 * @modified By
 */
@RestController
@RequestMapping("/search")
public class StudentController {

    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Resource
    private StudentService studentService;

    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void creatIndex(StudentDTO studentDTO) {
        Integer a = 100;
        String name = studentDTO.getName();
        for (int i = 0; i < a; i++) {
            studentDTO.setId(Long.valueOf(i));
            studentDTO.setName(name + i);
            studentService.createIndex(studentDTO, StudentDTO.INDEX_NAME, StudentDTO.TYPE);
        }
        logger.info("已创建");
    }

    @GetMapping("")
    @ResponseBody
    public List getAll() {
        // 查询所有
        List<SearchResult.Hit<StudentDTO, Void>> list = studentService.searchAll(StudentDTO.INDEX_NAME, new StudentDTO());

        logger.info("已查询");
        return list;
    }

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public JestResult list(StudentQuery query) {
        // 查询所有
        JestResult list = studentService.pagination(new StudentDTO(), query, StudentDTO.INDEX_NAME, StudentDTO.TYPE);

        logger.info("已查询");
        return list;
    }

    @GetMapping(value = "/getByKeyWord")
    @ResponseBody
    public List getByKeyWord(String keyWord) {
        // 根据关键字查询
        List<SearchResult.Hit<StudentDTO, Void>> list = studentService.createSearch(keyWord, StudentDTO.INDEX_NAME, new StudentDTO(), "name");

        logger.info("已查询");
        return list;
    }

    /**
     * 根据id删除
     *
     * @param id 删除条件
     */
    @DeleteMapping("/deleteById")
    public JestResult deleteById(String id) {
        return studentService.deleteDocumentById(StudentDTO.INDEX_NAME, StudentDTO.TYPE, id);
    }

    /**
     * 批量删除
     *
     * @param param 删除条件，json格式字符串，必须source包裹，es格式要求。如：
     *              {
     *              "query": {
     *              "match": {"name": "李四1"}
     *              }
     *              }
     *              或者
     *              {
     *              "query": {
     *              "match_all": {"id":"1", "name": "李四1"}
     *              }
     *              }
     */
    @DeleteMapping("/deleteByParam")
    public JestResult deleteByParam(String param) {
        return studentService.deleteDocumentByQuery(StudentDTO.INDEX_NAME, StudentDTO.TYPE, param);
    }

}
